<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="病区名称">
              <a-input style="width:100%" placeholder="请输入病区名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="医院名称">
              <a-select placeholder="全部医院" v-model="queryParam.hospitalId">
                <a-select-option :value="item.val" v-for="(item, index) in hospitalUl" :key="index">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="crudCreate()" type="primary" icon="plus">新增</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" bordered rowKey="id" :scroll="{ x: 1300 }" :columns="columns" :dataSource="tableData" :pagination="ipagination" :loading="loading" @change="handleTableChange">
        <!--操作-->
        <span slot="action" slot-scope="text, record">
          <a-button type="primary" @click="crudUpdate(record)">编辑</a-button>
          <a-popconfirm title="确认失效?" ok-text="确定" cancel-text="取消" @confirm="crudDelete(record.id)">
            <a-button type="danger" icon="delete"></a-button>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <AreaListModal ref="updateForm" @closeModal="closeModal" />
  </a-card>
</template>

<script>
import AreaListModal from './modules/areaListModal';
import ApiTree from '@/api/hospital/areaList';
import hospitalList from '@/api/hospital/hospitalList';
import { Slime, crud } from '@/mixins/slime';
import { evaFiltersMixin } from '@/mixins/evaMixin';

export default {
  name: 'areaList',
  mixins: [Slime, evaFiltersMixin],
  components: {
    AreaListModal,
  },
  data() {
    return {
      tableData: [],
      columns: [
        {
          title: '病区id',
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: '病区名字',
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: '所属医院',
          align: 'center',
          dataIndex: 'hospitalName',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      hospitalUl: [],
    };
  },
  created() {
    this.loadData();
    this.initHospital();
  },
  methods: {
    initHospital() {
      const that = this;
      hospitalList.getList().then((res) => {
        that.hospitalUl = res.result.records.map((obj, i) => {
          return (obj = {
            name: obj.name,
            val: obj.id,
          });
        });
      });
    },
    // 加载表格数据
    loadData(arg) {
      crud(ApiTree);
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData;
      });
    },
  },
};
</script>
<style scoped>
</style>